/**
* Created by huangjihui on 2017/9/7.
*/

var url = location.href.split("/");
var baseUrl = "http://" + url[2];
var browserCfg = {}; 
var ua = window.navigator.userAgent;
var maxFileSize = 50 * 1024 * 1000;
var allType = "bmp.jpg.jpeg.gif.png.doc.docx.xlx.xlsx.ppt.pptx.vsd.pdf.txt.mpp.xml.htm.html.xhtml.rar.zip";
if (ua.indexOf("MSIE")>=1){ 
  browserCfg.ie = true; 
}else if(ua.indexOf("Firefox")>=1){ 
  browserCfg.firefox = true; 
}else if(ua.indexOf("Chrome")>=1){ 
  browserCfg.chrome = true; 
} 
$.fn.getFormData = function(){
	var $this = $(this);
	var data = {};
	$.each($this.find("input"),function(){
		data[$(this).attr("name")] = $(this).val();
	});
	$.each($this.find("textarea"),function(){
		data[$(this).attr("name")] = $(this).val();
	});
	return data;
}
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)

// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

function loadStyles(url) {
	var link = document.createElement("link");
	link.type = "text/css";
	link.rel = "stylesheet";
	link.href = url;
	document.getElementsByTagName("head")[0].appendChild(link);
}

function StringToDate(DateStr, format) {

  var str=DateStr;
  if(str==null)
    return null;
  if(str.trim()=="")return null;
  var f=format || 'yyyy-MM-dd';
  var y4='([0-9]{4})';var y2='([0-9]{2})';var yi=-1;
  var M2='(0[1-9]|1[0-2])';var M1='([1-9]|1[0-2])';var Mi=-1;
  var d2 = '(0[1-9]|[1-2][0-9]|30|31)';var d1 = '([1-9]|[1-2][0-9]|30|31)';var di = -1;
  var H2 = '([0-1][0-9]|20|21|22|23)';var H1 = '([0-9]|1[0-9]|20|21|22|23)';var Hi = -1;
  var m2 = '([0-5][0-9])';var m1 = '([0-9]|[1-5][0-9])';var mi = -1;
  var s2 = '([0-5][0-9])';var s1 = '([0-9]|[1-5][0-9])';var si = -1;
  var regexp;
  if(function(){
        var reg=f;
        reg=reg.replace(/yyyy/,y4).replace(/yy/,y2).replace(/MM/,M2).replace(/M/, M1).replace(/dd/, d2).replace(/d/, d1).replace(/HH/, H2).replace(/H/, H1).replace(/mm/, m2).replace(/m/, m1).replace(/ss/, s2).replace(/s/, s1);
        regexp=new RegExp("^"+reg+"$");
        return regexp.test(str);
      }()){
    var index=function(formatString){
      var ia = new Array();var i = 0;
      yi =   formatString.search(/yyyy/);
      if ( yi < 0 ) yi = formatString.search(/yy/);
      if (yi >= 0) {ia[i] = yi;i++;}
      Mi =   formatString.search(/MM/);
      if ( Mi < 0 ) Mi = formatString.search(/M/);
      if (Mi >= 0) {ia[i] = Mi;i++;}
      di =   formatString.search(/dd/);
      if ( di < 0 ) di = formatString.search(/d/);
      if (di >= 0) {ia[i] = di;i++;}
      Hi =   formatString.search(/HH/);
      if ( Hi < 0 ) Hi = formatString.search(/H/);
      if (Hi >= 0) {ia[i] = Hi;i++;}
      mi =   formatString.search(/mm/);
      if ( mi < 0 ) mi = formatString.search(/m/);
      if (mi >= 0) {ia[i] = mi;i++;}
      si =   formatString.search(/ss/);
      if ( si < 0 ) si = formatString.search(/s/);
      if (si >= 0) {ia[i] = si;i++;}
      var ia2 = new Array(yi, Mi, di, Hi, mi, si);
      for(i=0; i<ia.length-1; i++){
        for(j=0;j<ia.length-1-i;j++){
          if(ia[j]>ia[j+1]) {temp=ia[j];ia[j]=ia[j+1];ia[j+1]=temp;}
        }
      }
      for (i=0; i<ia.length ; i++){
        for (j=0; j<ia2.length; j++){
          if(ia[i]==ia2[j]) {ia2[j] = i;}
        }
      }
      return ia2;
    }(f);
    var now = new Date();
    var vals = regexp.exec(str);
    var year = index[0]>=0?vals[index[0] + 1]:now.getFullYear();
    var month = index[1]>=0?(vals[index[1] + 1]-1):1;
    var day = index[2]>=0?vals[index[2] + 1]:1;
    var hour = index[3]>=0?vals[index[3] + 1]:"";
    var minute = index[4]>=0?vals[index[4] + 1]:"";
    var second = index[5]>=0?vals[index[5] + 1]:"";

    var validate;
    if (hour == ""){
      validate = new Date(year, month, day);}
    else
      validate = new Date(year, month, day, hour, minute, second);
    if(validate.getDate()==day) return validate;
  }else{
    return null;
  }
}

/**
 * async: 是否异步，true为异步，false为同步
 * */
function jquerypost(url, submitdata, async, func) {
  submitdata.requesttype='ajax';
  submitdata.Xtoken=sessionStorage.auth;
  jQuery.ajax({
    url: basePath + url,
    accepts: {
      mycustomtype: 'application/x-www-form-urlencoded'
    },
    data: submitdata,
    async: async,
    method: 'post',
    dataType: 'json',
    timeout: 60000,
    cache: false,
    success: function(data) {
      //  console.log(angular.toJson(data));
        func(data);
    }
  });
}

function post($http,url,data){
  if (data == undefined || data == null) {
    data = {};
  }
  data.requesttype='ajax';
  data.Xtoken=sessionStorage.auth;
  return $http.post(basePath+url,data,{withCredentials: true})
    .success(function(data,status){
    })
    .error(function (err, status, headers, config) { //这里的state就是0 需要添加http拦截器 做通讯异常统一处理
        if(err!=null&&(err.message==''||err.message==null)){
          err.message='系统错误，请稍后再试！';
        }
    })
    ;
}

function get($http, url) {
  url = basePath + url;
  if (url.indexOf('?') != -1) {
    url += '&requesttype=ajax';
  } else {
    url += '?requesttype=ajax';
  }
  return $http.get(url, {withCredentials: true});
}

/**
 * @Description: 格式化金额
 * */
function formatCurrency(num) {
  if(num == undefined || num == null || num == '') {
    return '0.00';
  } else {
    if(parseFloat(num)>-0.01 && parseFloat(num)<0.0) {
      return "0.00";
    } else {
      num = num.toString().replace(/\$|\,/g, '');
      if (isNaN(num)) {
        num = "0.00";
      } else {
        var sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        var cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10) {
          cents = "0" + cents;
        }

        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++) {
          num = num.substring(0, num.length - (4 * i + 3)) + ',' +
              num.substring(num.length - (4 * i + 3));
        }

        return "" + (((sign) ? '' : '-') + '' + num + '.' + cents);
      }
    }
  }
}

/**
 * @Description: 格式化百分比
 * */
function formatPercent(num) {
  if(!/\d+(\.\d+)?/.test(num)){
    num = 0;
  }
  var result = (num * 100).toString(),
      index = result.indexOf(".");
  if(index == -1 || result.substr(index+1).length <= 4){
    return result + "%";
  }
  return result.substr(0, index + 5) + "%";
}
function popup(toaster,level,title,message){
   toaster.pop(level, title, message);
}
